package com.lmqb.controller;

import com.lmqb.bean.JCollection;
import com.lmqb.bean.LmLoan;
import com.lmqb.bean.LmUser;
import com.lmqb.bean.SysUser;
import com.lmqb.entry.MenuTitle;
import com.lmqb.page.RepayPager;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.RepayService;
import com.lmqb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class RepayController extends BaseController{

	@Autowired
	private JiaoYanService jiaoYanService;
	
	@Autowired
	private RepayService repayService;
	
	@Autowired
	private UserService userService;
	//逾期用户
	@RequestMapping(value="overdue.html",method=RequestMethod.GET)
	public String toOverdue(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,RepayPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(RepayPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			pagerModel.setIsOver(2);
			RepayPager userBankPager=repayService.selectRepayPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("还款管理");
			menuTitle.setMenu("逾期用户");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "overdueList";
	}
	
	//调到添加催收记录页面
	@RequestMapping("addCuishou")
	public String addCuishou(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,RepayPager pagerModel){
		String repayNo = request.getParameter("repayNo");
		String loanNo = request.getParameter("loanNo");
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(RepayPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			pagerModel.setIsOver(3);
			pagerModel.setRepayNo(repayNo);
			RepayPager userBankPager=repayService.selectRepayPager(pagerModel);
			System.out.println(userBankPager.getList().get(0));
			LmUser lmUser = userService.selectByUserId(userBankPager.getList().get(0).getUserId());
			LmLoan lmLoan = repayService.selectByPrimaryKey(loanNo);
			request.setAttribute("LmLoan",lmLoan);
			request.setAttribute("LmUser",lmUser);
			request.setAttribute("List",userBankPager.getList().get(0));
			request.setAttribute("yuqi",userBankPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("逾期记录");
			menuTitle.setMenu("逾期用户");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "addCuishou";
	}
	//添加催收记录
	@RequestMapping(value="jiaCuishou",method=RequestMethod.POST)
	public String jiaCuishou(JCollection collection){
		try {
			System.out.println("催收记录"+collection);
			Byte b=1;
			collection.setStatus(b);
			int insert = repayService.insert(collection);
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return "redirect:overdue.html";
		}
}
